<cfcomponent output="false">
	<cfset $ = application.helpers />
	
	<!--- --->
	
	<cffunction name="getUserByID" access="public" output="false" returntype="query">
		<cfargument name="id" required="true" />

		<cfset var local = {} />
		
		<cfif arguments.id eq "" or arguments.id eq "-1">
			<cfset local.getUserByID = query(
				id:[],
				first_name:[],
				middle_name:[],
				last_name:[],
				street_1:[],
				street_2:[],
				payments:[],
				funds:[],
				city:[],
				state_abbrev:[],
				state_id:[],
				zip:[],
				email:[]
			) />
		<cfelse>
			<cfquery name="local.getUserByID" datasource="fundrdb">
				select 
				hex(user.id) as id,
				first_name,
				middle_name,
				last_name,
				street_1,
				street_2,
				123 as payments,
				ifNull(funds.total,0) as funds,
				city,
				hex(state_id) as state_id,
				state.abbrev as state_abbrev,
				zip,
				email
				from user 
				left join state on state.id = user.state_id
				left join (
					select fund.user_id,
					sum(payment_activity.amount) as total
					from fund
					inner join payment on payment.fund_id = fund.id
						and payment.active = 1
					inner join payment_activity on payment_activity.payment_id = payment.id
					group by fund.user_id
				) funds on funds.user_id = user.id
				where user.id = #$.database.uuid(arguments.id)#
				and user.active = 1
			</cfquery>
		</cfif>

		<cfreturn local.getUserByID />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="getUserByEmail" access="public" output="false" returntype="query">
		<cfargument name="email" required="true" />
		
		<cfset var local = {} />
		
		<cfquery name="local.getUserByID" datasource="fundrdb">
			select 
			hex(id) as id,
			first_name,
			middle_name,
			last_name,
			street_1,
			street_2,
			city,
			hex(state_id) as state_id,
			zip,
			email,
			password
			from user where email = '#arguments.email#'
			and active = 1
		</cfquery>

		<cfreturn local.getUserByID />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="getUserByFacebookID" access="public" output="false" returntype="query">
		<cfargument name="uid" required="true" />
		
		<cfset var local = {} />
		
		<cfquery name="local.getUserByFacebookID" datasource="fundrdb">
			select 
			hex(id) as id,
			first_name,
			middle_name,
			last_name,
			street_1,
			street_2,
			city,
			hex(state_id) as state_id,
			zip,
			email,
			password
			from user where fb_id = '#arguments.uid#'
			and active = 1
		</cfquery>

		<cfreturn local.getUserByFacebookID />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="login" access="public" output="false" returntype="query">
		<cfargument name="email" required="true" />
		<cfargument name="password" required="true" />
		
		<cfset arguments.password = $.format.encrypt(arguments.password) />
		
		<cfset var local = {} />
		
		<cfquery name="local.getUserByID" datasource="fundrdb">
			select 
			hex(id) as id,
			first_name,
			middle_name,
			last_name,
			street_1,
			street_2,
			city,
			hex(state_id) as state_id,
			zip,
			email,
			password
			from user where email = #$.database.string(arguments.email)#
			and password = #$.database.string(arguments.password)#
			and active = 1
		</cfquery>

		<cfreturn local.getUserByID />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="getFavorites" access="public" output="false" returntype="query">
		<cfargument name="userID" required="true" />
		
		<cfset var local = {} />
		
		<cfquery name="local.getFavorites" datasource="fundrdb">
			select 
			hex(fund_id) as fund_id
			from fund_favorite
			where user_id = #$.database.uuid(arguments.userID)#
			and active = 1
		</cfquery>

		<cfreturn local.getFavorites />
		
	</cffunction>
	
	<!--- --->
	
	<cffunction name="removeFavorite" access="public" output="false" returntype="boolean">
		<cfargument name="fundID" required="true" />
		
		<cfset var local = {} />
		
		<cfquery name="local.removeFavorite" datasource="fundrdb">
			update fund_favorite
			set active = 0
			where active = 1
			and fund_id = #$.database.uuid(arguments.fundID)#
			and user_id = #$.database.uuid(request.session.user.id)#
		</cfquery>
		
		<cfreturn true />
		
	</cffunction>
	
	<!--- --->

</cfcomponent>